#include "thread.hpp"
#include <memory>
#include <unistd.h>
#include <vector>
#include <string>
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
int tickets = 10000;
void *getTicket(void *args)
{
    std::string username = static_cast<const char *>(args);
    while (true)
    {
        pthread_mutex_lock(&lock);
        if (tickets > 0)
        {
            usleep(1254);
            printf("%s : 正在进行抢票 : %d\n", username.c_str(), tickets--);
            pthread_mutex_unlock(&lock);
        }
        else
        {
            pthread_mutex_unlock(&lock);
            break;
        }
    }
    return nullptr;
}
void test()
{
    std::unique_ptr<Thread> thread1(new Thread(getTicket, (void *)"user1", 1));
    std::unique_ptr<Thread> thread2(new Thread(getTicket, (void *)"user2", 2));
    std::unique_ptr<Thread> thread3(new Thread(getTicket, (void *)"user3", 3));
    std::unique_ptr<Thread> thread4(new Thread(getTicket, (void *)"user4", 4));
    thread1->join();
    thread2->join();
    thread3->join();
    thread4->join();
}
int tickets1 = 10000;
class ThreadData
{
public:
    ThreadData(const std::string &threadname, pthread_mutex_t *mutex)
        : threadname_(threadname), mutex_(mutex)
    {
    }
    ~ThreadData()
    {
    }

public:
    std::string threadname_;
    pthread_mutex_t *mutex_;
};
void *getTicket1(void *args)
{
    ThreadData *td = static_cast<ThreadData *>(args);
    while (true)
    {
        pthread_mutex_lock(td->mutex_);
        if (tickets1 > 0)
        {
            usleep(1254);
            std::cout << td->threadname_ << " :正在进行抢票: " << tickets1 << std::endl;
            tickets1--;
            pthread_mutex_unlock(td->mutex_);
        }
        else
        {
            pthread_mutex_unlock(td->mutex_);
            break;
        }
        usleep(1000);
    }
    return nullptr;
}
void test1()
{
#define NUM 4
    pthread_mutex_t lock1;
    pthread_mutex_init(&lock1, nullptr);
    std::vector<pthread_t> tids(NUM);
    for (int i = 0; i < NUM; i++)
    {
        char buffer[1024];
        snprintf(buffer, sizeof(buffer), "thread %d", i + 1);
        ThreadData *td = new ThreadData(buffer, &lock1);
        pthread_create(&tids[i], nullptr, getTicket1, (void *)td);
    }
    for (const auto &tid : tids)
    {
        pthread_join(tid, nullptr);
    }
    pthread_mutex_destroy(&lock1);
}
pthread_mutex_t lock2 = PTHREAD_MUTEX_INITIALIZER;
int tickets2 = 10000;
void *getTicket2(void *args)
{
    std::string username = static_cast<const char *>(args);
    while (true)
    {
        pthread_mutex_lock(&lock2);
        if (tickets2 > 0)
        {
            usleep(1254);
            printf("%s : 正在进行抢票 : %d\n", username.c_str(), tickets2--);
            pthread_mutex_unlock(&lock2);
        }
        else
        {
            pthread_mutex_unlock(&lock2);
            break;
        }
        usleep(1000);
    }
    return nullptr;
}
void test2()
{
    pthread_t t1, t2, t3, t4;
    pthread_create(&t1, nullptr, getTicket2, (void *)"thread 1");
    pthread_create(&t2, nullptr, getTicket2, (void *)"thread 2");
    pthread_create(&t3, nullptr, getTicket2, (void *)"thread 3");
    pthread_create(&t4, nullptr, getTicket2, (void *)"thread 4");
    pthread_join(t1, nullptr);
    pthread_join(t2, nullptr);
    pthread_join(t3, nullptr);
    pthread_join(t4, nullptr);
}
int main()
{
    // test();
    // test1();
    test2();
    return 0;
}